package web.servlet.userlist.web.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @Author Administrator
 * @Date 2020/6/27 14:09
 **/
@WebFilter("/*")
public class LoginFilter implements Filter {
    @Override
    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest request = (HttpServletRequest) req;
        String uri = request.getRequestURI();
        if (uri.contains("/login.jsp") || uri.contains("/Login") || uri.contains("/checkCode") || uri.contains("/css/")
                || uri.contains("/js/") || uri.contains("/fonts/")) {
            chain.doFilter(req, resp);
        } else {
            Object user = request.getSession().getAttribute("user");
            if(user!=null){
                chain.doFilter(req, resp);
            }else {
                request.setAttribute("login_msg","您未登录");
                request.getRequestDispatcher("/userlist/login.jsp").forward(req,resp);
            }
        }

    }

    @Override
    public void init(FilterConfig config) throws ServletException {

    }

}
